import mx.utils.Delegate;

import gugga.common.EventDescriptor;
import gugga.common.ITask;
import gugga.components.Button;
import gugga.debug.Assertion;
import gugga.sequence.ExecuteAsyncMethodTask;
import gugga.sequence.ExecuteMethodTask;
import gugga.sequence.TaskSequence;
import gugga.utils.Listener;

import useOfSequences.CommonSection;

/**
 * @author bony
 * This section has only one sequence,but the user can add some tasks(animations).
 * The animations can be added in arbitrary sequence and every animation can be added more than once.
 * The user adds task,when click on some animation.  
 */ 
class useOfSequences.ManagingSection extends CommonSection 
{
	// List the ManagingSection fields here.
	private var mPlayButton: Button;
	private var mStopButton: Button;	
	
	private var mTitleAnimButton: Button;
	private var mMailAnimButton: Button;
	private var mLogoAnimButton: Button;
	private var mCupAnimButton: Button;
	private var mEggsAnimButton: Button;
	
	private var mDisablePlayButtonTask: ITask;
	private var mEnablePlayButtonTask: ITask;
		
	private var mLinearSequence: TaskSequence;
	
	public function ManagingSection() 
	{
		super();
		
		// Get the instances from the scene.
		mPlayButton = getComponent("btnPlay");		
		mStopButton = getComponent("btnStop");	
		
		mTitleAnimButton = getComponent("TitleAnimButton");
		mMailAnimButton = getComponent("MailAnimButton");
		mLogoAnimButton = getComponent("LogoAnimButton");
		mCupAnimButton = getComponent("CupAnimButton");
		mEggsAnimButton = getComponent("EggsAnimButton");		
	}
	
	public function initUI()
	{
		super.initUI();		
		
		// Create task that will disable the play button.
		// This task will be included in appropriate place in mLinearSequence.
		mDisablePlayButtonTask = ExecuteMethodTask.create(mPlayButton, mPlayButton.disable, null);
		// Create task that will enable the play button.
		// This task will be included in appropriate place in mLinearSequence.
		mEnablePlayButtonTask = ExecuteMethodTask.create(mPlayButton, mPlayButton.enable, null);
		
		// Linear sequence is simple sequence that executes the animations one by one.  	
		mLinearSequence = prepareLinearSequence();
		
		// Add event listener to the "mPlayButton" and execute "onPlayButtonClick", that starts the sequence.
		mPlayButton.addEventListener("click", Delegate.create(this, onPlayButtonClick));
		// Add event listener to the "mStopButton" and execute "onStopButtonClick", that stops the sequence.
		mStopButton.addEventListener("click", Delegate.create(this, onStopButtonClick));
		
		// Create listener that on event "click" of the buttons, executes method "onSectorButtonClick".
		// Use "Listener.createMergingListener" because we need to add dynamically argument (id) to the event info object. 
		// In "onSectorButtonClick" depending of the id, we are adding new task (animation) in mLinearSequence.  
		Listener.createMergingListener(new EventDescriptor(mTitleAnimButton, "click"), Delegate.create(this, onSectorButtonClick), {id:"titleAnim"});
		Listener.createMergingListener(new EventDescriptor(mMailAnimButton, "click"), Delegate.create(this, onSectorButtonClick), {id:"mailAnim"});
		Listener.createMergingListener(new EventDescriptor(mLogoAnimButton, "click"), Delegate.create(this, onSectorButtonClick), {id:"logoAnim"});
		Listener.createMergingListener(new EventDescriptor(mCupAnimButton, "click"), Delegate.create(this, onSectorButtonClick), {id:"cupAnim"});
		Listener.createMergingListener(new EventDescriptor(mEggsAnimButton, "click"), Delegate.create(this, onSectorButtonClick), {id:"eggsAnim"});				
	}

	private function prepareLinearSequence() : TaskSequence
	{		
		var linearSequence : TaskSequence = new TaskSequence();
		linearSequence.addTask(mDisablePlayButtonTask);
		linearSequence.addTask(mTitleAnim);
		linearSequence.addTask(mMailAnim);
		linearSequence.addTask(mLogoAnim);
		linearSequence.addTask(mCupAnim);
		linearSequence.addTask(mEggsAnim);
		linearSequence.addTask(mEnablePlayButtonTask);
	
		return linearSequence;	
	}
	
	private function onPlayButtonClick(ev) : Void
	{
		mLinearSequence.start();
	}
	
	private function onStopButtonClick(ev) : Void
	{
		// To stop the sequence we must interrupt it, enable the button play 
		// and hide all the animations.
		
		mLinearSequence.interrupt();
		mPlayButton.enable();
		
		mTitleAnim.hide();
		mMailAnim.hide();
		mLogoAnim.hide();
		mCupAnim.hide();
		mEggsAnim.hide();
	}
	
	private function onSectorButtonClick(ev) : Void
	{
		// Every time an animation button is clicked, we should create a new task.
		var newTask : ITask;
		
		// Depending of the id, set the newTask as an new ExecuteAsyncMethodTask, that will complete when the animation completes.
		// Use ExecuteAsyncMethodTask.create() to create and initialize an ExecuteAsyncMethodTask instance. 
		switch(ev.id)
		{
			case "titleAnim": 
				newTask = ExecuteAsyncMethodTask.create(
					new EventDescriptor(mTitleAnim, "completed"), 
					mTitleAnim, mTitleAnim.start, null);
			break;
			case "mailAnim": 
				newTask = ExecuteAsyncMethodTask.create(
					new EventDescriptor(mMailAnim, "completed"), 
					mMailAnim, mMailAnim.start, null);				
			break;
			case "logoAnim": 
				newTask = ExecuteAsyncMethodTask.create(
					new EventDescriptor(mLogoAnim, "completed"), 
					mLogoAnim, mLogoAnim.start, null);				
			break;
			case "cupAnim": 
				newTask = ExecuteAsyncMethodTask.create(
					new EventDescriptor(mCupAnim, "completed"), 
					mCupAnim, mCupAnim.start, null);			
			break;
			case "eggsAnim": 
				newTask = ExecuteAsyncMethodTask.create(
					new EventDescriptor(mEggsAnim, "completed"), 
					mEggsAnim, mEggsAnim.start, null);				
			break;
			default:
				Assertion.fail("Unknown button hit", this, arguments);
			break;
			
		}
		
		// Add the newTask before the mEnablePlayButtonTask in mLinearSequence,
		// because the play button must stay disable, until the sequence completes. 
		mLinearSequence.addTaskBefore(mEnablePlayButtonTask, newTask);
	}	
}